<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2018\10\28 0028
 * Time: 10:03
 */
namespace app\admin\controller;
use app\admin\model\Databases;
use app\admin\model\NewBuilder;
use think\Db;
use think\Request;
use think\Validate;

class Model
{
    /**加载列表
     * @param Request $request
     * @return \think\response\View
     */
    public function show_list(Request $request){
        $tiaojian = $request->param('tiaojian',null);
        if($tiaojian){
            $data = Db::name('model')->where('model_name|surface','like',"%$tiaojian%")->select();
            $datarows = count($data);
        }else{
            $data = Db::name('model')->select();
            $datarows = count($data);
        }
        return view('list',['data'=>$data,'datarows'=>$datarows,'tiaojian'=>$tiaojian]);
    }

    /**加载添加模型
     * @param Request $request
     * @return \think\response\View
     */
    public function show_add(Request $request){
        return view('add');
    }

    /**添加模型
     * @param Request $request
     * @return array|int|string
     */
    public function add(Request $request){
        $data = $request->param();
        $val = new Validate([
            'model_name'=>'require',
            'surface'=>'require'
        ]);
//        echo '<pre>';print_r($data);exit;
        if(isset($data['/admin/model/add_html'])){
            unset($data['/admin/model/add_html']);
        }

        if(!$val->check($data)){
            return $val->getError();
        }
//        echo '<pre>';print_r($data);exit;
        $bool = Databases::add_table($data);
//        echo '<pre>';print_r($bool);exit;
        if($bool&&Db::name('model')->insert($data)){
            return 1;
        }else{
            return '添加失败';
        }
    }

    /**加载编辑
     * @param Request $request
     * @return string|\think\response\View
     */
    public function edit(Request $request){
        $id = $request->param('id',null);
        if(!$id){
            return '请输入修改的id';
        }
        $data = Db::name('model')->where('id',$id)->find();
        return view('edit',['data'=>$data]);
    }

    /**更新
     * @param Request $request
     * @return array|int|string
     */
    public function update(Request $request){
        $data = $request->post();
        $val = new Validate([
            'model_name'=>'require',
            'surface'=>'require'
        ]);
//        return $data;
        if(!$val->check($data)){
            return $val->getError();
        }
        $bool = Databases::update_table($data);
        if($bool&&Db::name('model')->update($data)){
            return 1;
        }else{
            return '修改失败';
        }
    }

    /**删除模型
     * @return int|string
     */
    public function del(){
        $id = $_POST['id'];
        if(!$id){
            return '请输入删除的id';
        }
        $table = Db::name('model')->where('id','in',$id)->select();
        foreach ($table as $value){
            $bool = Databases::del_table($value['surface']);
            if(!$bool){
                return '删除失败';
            }
        }
        $shuju =Db::name('table_field')->where('modelid','in',$id)->select();
        if(count($shuju)){
            $bools = Db::name('table_field')->where('modelid','in',$id)->delete();
            if(!$bools){
                return '删除失败';
            }
        }
        if(Db::name('model')->where('id','in',$id)->delete()){
            return 1;
        }else{
            return '删除失败';
        }
    }

    /**加载字段列表页面
     * @param Request $request
     * @return string|\think\response\View
     */
    public function field(Request $request){
        $id = $request->param('id',null);
        if(!$id){
            return '请输入要编辑字段的id';
        }
        $datas = Db::name('model')->where('id',$id)->find();
//        echo '<pre>';
//        print_r($datas);exit;
        $name = $datas['surface'];
        $data = Db::name('table_field')->where('modelid',$id)->select();
//                echo '<pre>';
//        print_r($data);exit;
        return view('table_list',['data'=>$data,'name'=>$name,'id'=>$id]);
    }
}